package com.buluo.service.impl

import com.aliyun.oss.OSSClient
import com.buluo.service.ImgService
import com.buluo.utils.AliOSSUtil
import com.mongodb.DBCollection
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

import javax.servlet.http.HttpServletRequest

/**
 * Created by sunhao on 2016/12/15.
 */
@Service
class ImgServiceImpl extends BaseMongoServiceImpl implements ImgService {

//    @Autowired
//    OSSClient client
//
//
//    @Override
//    String uploadImg(File file) throws Exception {
//        String path = "img/${file.getName()}"
//        AliOSSUtil.uploadFile(client, path, file)
//        return path
//    }

    @Override
    String uploadImg(MultipartFile file)throws Exception {
        //TODO 修改基础路径
//        String pathRoot = "/home"
        String pathRoot = "C:\\database"
        String path="";
        if(!file.isEmpty()){
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType=file.getContentType();
            //获得文件后缀名称
            String imageName=contentType.substring(contentType.indexOf("/")+1);
            //TODO 修改路径符号
//            path="/1905hd/images/"+uuid+"."+imageName;
            path="\\1905hd\\images\\"+uuid+"."+imageName;
            file.transferTo(new File(pathRoot+path));
        }
        println(path);
        return path
    }



    DBCollection getColl() {
        hdMongoTemplate.getCollection("hd_imgs")
    }
}
